import { ApiRes } from '@/types/data'
import { Profile } from '@/types/user'
import request from '@/utils/request'
import { getProfile, removeProfile, setProfile } from '@/utils/storage'
import { defineStore } from 'pinia'
export default defineStore('user',{
  state() {
      return{
          profile:getProfile() as Profile
      }
  },
  actions:{
    async login(data:{ account: string, password: string}){
      const res = await request.post<ApiRes<Profile>>('/login',data)
      this.profile = res.data.result
      setProfile(this.profile)
    },
    async getCode(mobile :string){
      await request.get('/login/code',{
        params:{
          mobile
        }
      })
    },
    async mobailLogin(data:{ mobile: string, code: string}){
      const res =  await request.post<ApiRes<Profile>>('/login/code',data)
      this.profile = res.data.result
      setProfile(this.profile)


    },
    logout(){
      this.profile = {} as Profile
      removeProfile()
    },
    async qqLogin( data:{ unionId:string,source:number }){
    const res = await  request.post<ApiRes<Profile>>('/login/social',data)
    this.profile = res.data.result
    setProfile(this.profile)

    },
    async bindSms(data:{  unionId: string, mobile: string, code: string}){
      const res  = await request.post<ApiRes<Profile>>('/login/social/bind',data)
      this.profile = res.data.result
      setProfile(this.profile)
    },
    async bindQQsms(mobile:string){
       await request.get('/login/social/code',{
      params:{
          mobile
      }
      })
    },
    async sendQQPathMsg(mobile: string) {
      await request.get('/register/code', {
        params: {
          mobile
        }
      })
    },
    
    async qqPatchLogin(data: any) {
      const res = await request.post<ApiRes<Profile>>(
        `/login/social/${data.openId}/complement`,
        data
      )
      // 1. 保存用户信息到 state 中
      this.profile = res.data.result
      setProfile(res.data.result)
    }
  }
})